﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using HMEngine.HMComponents;

namespace HMEngine.HMObjects {
    public class HMQuad : HMObject, IHMRenderable, IHMLoadable {
        private readonly string myAsset;
        private readonly Color myColor;
        private Texture2D myTexture;
        private VertexPositionColorTexture[] myVertices;
        private int[] myIndexes;

        public string Shader { get; set; }
        public VertexDeclaration VertexDeclaration { get; private set; }

        public HMQuad(string textureAsset, Color color) {
            myAsset = textureAsset;
            myColor = color;
        }

        public void LoadContent(GraphicsDevice myDevice, ContentManager myLoader) {
            myTexture = myLoader.Load<Texture2D>(myAsset);
            VertexDeclaration = new VertexDeclaration(myDevice, VertexPositionColorTexture.VertexElements);

            myVertices = new[] {
                new VertexPositionColorTexture(new Vector3(-0.5f, 0.5f, 0), myColor, new Vector2(0, 0)),
                new VertexPositionColorTexture(new Vector3(0.5f, 0.5f, 0), myColor, new Vector2(1, 0)),
                new VertexPositionColorTexture(new Vector3(-0.5f, -0.5f, 0), myColor, new Vector2(0, 1)),
                new VertexPositionColorTexture(new Vector3(0.5f, -0.5f, 0), myColor, new Vector2(1, 1))
            };

            myIndexes = new[] { 0, 1, 2, 1, 3, 2 };
        }

        public void Render(GraphicsDevice myDevice) {
            myDevice.Textures[0] = myTexture;
            myDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, myVertices, 0, 4, myIndexes, 0, 2);
        }
    }
}